﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using MP3Ganizer.BusinessObject.Infos;
using MP3Ganizer.BusinessObject.Hierarchy;
using MP3Ganizer.BusinessObject.Services.Classification;

namespace MP3Ganizer.BusinessObject.Services.Test
{
    [TestClass]
    public class Classification
    {
        [TestMethod]
        public void TryToClassify_ByArtist()
        {
            MP3Infos info1 = new MP3Infos(new List<string>(){"toto"}, "A", "A",1, "c:/a/b/c.mp3", new string[0]);
            MP3Infos info2 = new MP3Infos(new List<string>() { "toto" }, "A", "B", 2, "c:/a/b/c.mp3", new string[0]);
            MP3Infos info3 = new MP3Infos(new List<string>() { "titi" }, "B", "A", 1, "c:/b/a/c.mp3", new string[0]);
            MP3Infos info4 = new MP3Infos(new List<string>() { "titi" }, "B", "B", 2, "c:/b/a/c.mp3", new string[0]);

            ClassificationService service = new ClassificationService();

            
            List<MP3Infos> infos = new List<MP3Infos>(){
            info1,
            info2,
            info3,
            info4,
            };

            var maybeClassifiedList = service.ClassifyByArtists(infos.AsEnumerable());


            Assert.IsTrue(maybeClassifiedList.Count() == 2);
            Assert.AreEqual("titi", maybeClassifiedList.ElementAt(0).Label);
            Assert.AreEqual("toto", maybeClassifiedList.ElementAt(1).Label);
            
            Assert.IsTrue(maybeClassifiedList.ElementAt(0).Children.Count() == 1);
            Assert.IsTrue(maybeClassifiedList.ElementAt(1).Children.Count() == 1);
            Assert.AreEqual("B", maybeClassifiedList.ElementAt(0).Children.ElementAt(0).Label);
            Assert.AreEqual("A", maybeClassifiedList.ElementAt(1).Children.ElementAt(0).Label);

            Assert.AreEqual("A", maybeClassifiedList.ElementAt(0).Children.Select(c=> (FolderModel)c).ElementAt(0).Children.ElementAt(0).Label);
            Assert.AreEqual("B", maybeClassifiedList.ElementAt(0).Children.Select(c => (FolderModel)c).ElementAt(0).Children.ElementAt(1).Label);
            Assert.AreEqual("A", maybeClassifiedList.ElementAt(1).Children.Select(c => (FolderModel)c).ElementAt(0).Children.ElementAt(0).Label);
            Assert.AreEqual("B", maybeClassifiedList.ElementAt(1).Children.Select(c => (FolderModel)c).ElementAt(0).Children.ElementAt(1).Label);
        }

        [TestMethod]
        public void TryToClassify_ByAlbum()
        {
            MP3Infos info1 = new MP3Infos(new List<string>() { "toto" }, "A", "A", 1, "c:/a/b/c.mp3", new string[0]);
            MP3Infos info2 = new MP3Infos(new List<string>() { "toto" }, "A", "B", 2, "c:/a/b/c.mp3", new string[0]);
            MP3Infos info3 = new MP3Infos(new List<string>() { "titi" }, "B", "A", 1, "c:/b/a/c.mp3", new string[0]);
            MP3Infos info4 = new MP3Infos(new List<string>() { "titi" }, "B", "B", 2, "c:/b/a/c.mp3", new string[0]);

            ClassificationService service = new ClassificationService();


            List<MP3Infos> infos = new List<MP3Infos>(){
            info1,
            info2,
            info3,
            info4,
            };

            var maybeClassifiedList = service.ClassifyByAlbum(infos.AsEnumerable());


            Assert.IsTrue(maybeClassifiedList.Count() == 2);
            Assert.AreEqual("A", maybeClassifiedList.ElementAt(0).Label);
            Assert.AreEqual("B", maybeClassifiedList.ElementAt(1).Label);

            Assert.IsTrue(maybeClassifiedList.ElementAt(0).Children.Count() == 1);
            Assert.IsTrue(maybeClassifiedList.ElementAt(1).Children.Count() == 1);
            Assert.AreEqual("toto", maybeClassifiedList.ElementAt(0).Children.ElementAt(0).Label);
            Assert.AreEqual("titi", maybeClassifiedList.ElementAt(1).Children.ElementAt(0).Label);

            Assert.AreEqual("A", maybeClassifiedList.ElementAt(0).Children.Select(c => (FolderModel)c).ElementAt(0).Children.ElementAt(0).Label);
            Assert.AreEqual("B", maybeClassifiedList.ElementAt(0).Children.Select(c => (FolderModel)c).ElementAt(0).Children.ElementAt(1).Label);
            Assert.AreEqual("A", maybeClassifiedList.ElementAt(1).Children.Select(c => (FolderModel)c).ElementAt(0).Children.ElementAt(0).Label);
            Assert.AreEqual("B", maybeClassifiedList.ElementAt(1).Children.Select(c => (FolderModel)c).ElementAt(0).Children.ElementAt(1).Label);
        }

        [TestMethod]
        public void TryToClassify_ByDirectory()
        {
            MP3Infos info1 = new MP3Infos(new List<string>() { "toto" }, "A", "A", 1, "c:\\a\\b\\c.mp3", new string[0]);
            MP3Infos info2 = new MP3Infos(new List<string>() { "toto" }, "A", "B", 2, "c:\\a\\b\\c.mp3", new string[0]);
            MP3Infos info3 = new MP3Infos(new List<string>() { "titi" }, "B", "A", 1, "c:\\b\\a\\c.mp3", new string[0]);
            MP3Infos info4 = new MP3Infos(new List<string>() { "titi" }, "B", "B", 2, "c:\\b\\a\\c.mp3", new string[0]);

            ClassificationService service = new ClassificationService();


            List<MP3Infos> infos = new List<MP3Infos>(){
            info1,
            info2,
            info3,
            info4,
            };

            var maybeClassifiedList = service.ClassifyByDirectory(infos.AsEnumerable());


            Assert.IsTrue(maybeClassifiedList.Count() == 2);
            Assert.AreEqual("c:\\a\\b",maybeClassifiedList.ElementAt(0).Label);
            Assert.AreEqual("c:\\b\\a",maybeClassifiedList.ElementAt(1).Label);

            Assert.IsTrue(maybeClassifiedList.ElementAt(0).Children.Count() == 2);
            Assert.IsTrue(maybeClassifiedList.ElementAt(1).Children.Count() == 2);
            Assert.AreEqual("A", maybeClassifiedList.ElementAt(0).Children.ElementAt(0).Label);
            Assert.AreEqual("B", maybeClassifiedList.ElementAt(0).Children.ElementAt(1).Label);
            Assert.AreEqual("A", maybeClassifiedList.ElementAt(1).Children.ElementAt(0).Label);
            Assert.AreEqual("B", maybeClassifiedList.ElementAt(1).Children.ElementAt(1).Label);
        }
    }
}
